﻿using MiniExcelLibs;
using MiniExcelLibs.OpenXml;

namespace Digitalmes.Infrastructure.IO.Excel;

/// <summary>
/// MiniExcel 导出。
/// </summary>
public sealed class MiniExcelExport : IExcelExport
{
    public void SaveAs<T>(string path, IEnumerable<T> data, string sheetName = "Sheet1", bool isCSV = false)
    {
        var config = new OpenXmlConfiguration()
        {
            TableStyles = TableStyles.None,
            AutoFilter = false,
        };
        MiniExcel.SaveAs(path, data, sheetName: sheetName, excelType: !isCSV ? ExcelType.XLSX : ExcelType.CSV, configuration: config);
    }

    public void SaveAs<T>(Stream stream, IEnumerable<T> data, string sheetName = "Sheet1", bool isCSV = false)
    {
        var config = new OpenXmlConfiguration()
        {
            TableStyles = TableStyles.None,
            AutoFilter = false,
        };
        stream.SaveAs(data, sheetName: sheetName, excelType: !isCSV ? ExcelType.XLSX : ExcelType.CSV, configuration: config);
    }

    public async Task SaveAsAsync<T>(string path, IEnumerable<T> data, string sheetName = "Sheet1", bool isCSV = false, CancellationToken cancellationToken = default)
    {
        var config = new OpenXmlConfiguration()
        {
            TableStyles = TableStyles.None,
            AutoFilter = false,
        };
        await MiniExcel.SaveAsAsync(path, data, sheetName: sheetName, excelType: !isCSV ? ExcelType.XLSX : ExcelType.CSV, configuration: config, cancellationToken: cancellationToken);
    }

    public async Task SaveAsAsync<T>(Stream stream, IEnumerable<T> data, string sheetName = "Sheet1", bool isCSV = false, CancellationToken cancellationToken = default)
    {
        var config = new OpenXmlConfiguration()
        {
            TableStyles = TableStyles.None,
            AutoFilter = false,
        };
        await stream.SaveAsAsync(data, sheetName: sheetName, excelType: !isCSV ? ExcelType.XLSX : ExcelType.CSV, configuration: config, cancellationToken: cancellationToken);
    }
}
